import random

import pygame.sprite

# 窗口大小常量
WINDOW_SIZE = pygame.Rect(0, 0, 432, 644)
# 创造障碍物常量
CREATE = pygame.USEREVENT


class Backgroud(pygame.sprite.Sprite):
    """背景精灵"""

    def __init__(self, is_alt=False):
        super(Backgroud, self).__init__()
        # 加载图片
        self.image = pygame.image.load("images/bg.png")
        self.rect = self.image.get_rect()
        # 速度
        self.speed = 1
        # 判断是否为第二张图片
        if is_alt:
            self.rect.left = WINDOW_SIZE.right

    def update(self):
        """更新背景位置"""
        self.rect.x -= self.speed
        if self.rect.right <= WINDOW_SIZE.left:
            self.rect.left = WINDOW_SIZE.right


class Groud(pygame.sprite.Sprite):
    """地面精灵"""

    def __init__(self, is_alt=False):
        super(Groud, self).__init__()
        self.image = pygame.image.load("images/ground.png")
        self.rect = self.image.get_rect()
        self.rect.bottom = WINDOW_SIZE.bottom
        # 速度
        self.speed = 1
        # 判断是否为第二张图片
        if is_alt:
            self.rect.left = WINDOW_SIZE.right

    def update(self):
        """更新背景位置"""
        self.rect.x -= self.speed
        if self.rect.right <= WINDOW_SIZE.left:
            self.rect.left = WINDOW_SIZE.right


class Obstacle(pygame.sprite.Sprite):
    """水管障碍精灵"""

    def __init__(self):
        super(Obstacle, self).__init__()
        self.image = pygame.image.load("images/column.png")
        self.rect = self.image.get_rect()
        # 初始位置
        self.rect.x = WINDOW_SIZE.right
        self.rect.y = random.randint(-500, -250)
        # 初始速度
        self.speed = 1

    def update(self):
        self.rect.x -= self.speed
        if self.rect.right < WINDOW_SIZE.left:
            self.kill()


class Bird(pygame.sprite.Sprite):
    """小鸟精灵"""

    def __init__(self, screen):
        super(Bird, self).__init__()
        self.image = pygame.image.load("images/1.png")
        self.images_list = [pygame.image.load("images/1.png"),
                            pygame.image.load("images/2.png"),
                            pygame.image.load("images/3.png"),
                            pygame.image.load("images/4.png"),
                            pygame.image.load("images/5.png"),
                            pygame.image.load("images/6.png"),
                            pygame.image.load("images/7.png")]
        self.rect = self.image.get_rect()
        # 初始位置
        self.rect.x = 100
        self.rect.bottom = 489
        self.screen = screen
        # 索引
        self.index = 0
        # 速度
        self.speed = 0

    def update(self):
        self.rect.y += self.speed

    def move(self):
        """小鸟移动"""
        event_list = pygame.key.get_pressed()
        if event_list[pygame.K_SPACE]:
            self.speed = -5
        elif self.rect.bottom < 480:
            self.speed = 1
        elif self.rect.bottom > 489:
            self.speed = 0
            self.rect.bottom = 489

    def bird_display(self):
        self.screen.blit(self.images_list[self.index], (self.rect.x, self.rect.y))
        self.index += 1
        if self.index > 6:
            self.index = 0
